<template>
  <div>
    <form @submit.prevent="change(keywords)">
      <div class="input">
        <i></i>
          <input
            type="text"
            v-model="keywords"
            @input="inputing = true"
            placeholder="搜索歌曲，歌手，专辑"
          />
      </div>
    </form>
    <component
      :is="search"
      @play="$emit('play', $event)"
      :keywords="keywords"
      @change="change"
    ></component>
  </div>
</template>
<script>
import HotSearch from "@/components/HotSearch";
import SearchSuggest from "@/components/SearchSuggest";
import SearchResult from "@/components/SearchResult";
export default {
  name: "Search",
  data() {
    return {
      search: "HotSearch",
      keywords: "",
      inputing: false,
    };
  },
  created() {},
  methods: {
    change(val) {
      this.keywords = val;
      this.inputing = false;
      this.search = "SearchResult";
    },
  },
  watch: {
    keywords(val) {
      if (val == "") {
        this.search = "HotSearch";
      } else if (this.inputing) {
        this.search = "SearchSuggest";
      }
    },
  },
  components: {
    HotSearch,
    SearchSuggest,
    SearchResult,
  },
};
</script>
<style lang="less" scoped>
.input {
  position: relative;
  width: 100%;
  height: 30px;
  padding: 0 30px;
  box-sizing: border-box;
  background: #ebecec;
  border-radius: 30px;
  display: flex;
  align-items: center;
  i {
    display: inline-block;
    width: 13px;
    height: 13px;
    background-image: url();
    background-repeat: no-repeat;
  }
  input {
    flex: 1;
    background-color: transparent;
    outline: none;
    border: none;
    padding: 15px 10px;
  }
}
form{
  padding: 15px 10px;
  border-bottom: 1px solid #f5f6f7;
}
</style>